<?php
namespace app\admin\controller;

use app\common\model\Transfer as TransferModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Request;
/**
 * 会员积分互转
 * Class AdminUser
 * @package app\admin\controller
 */
class Transfer extends AdminBase
{
    protected $transfer_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->transfer_model = new TransferModel();
    }

    /**
     * 会员积分互转记录列表
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '',$wallet_type_id = '',$page = 1,$begin_time='',$end_time='')
    {
        $map_join = [];
        // 关键字查询条件                                         
        if ($keyword) {                                                     
            $map_join['u.user_name|u.mobile|u.email|us.user_name|us.mobile|us.email'] = ['like', "%{$keyword}%"];
        }
        // 钱包类型搜索
        if (isNum($wallet_type_id)) {                                       
            $map_join['t.wallet_type'] = ['=', "$wallet_type_id"];          
        }
        // 时间检索
        if($begin_time && $end_time){                                                    
            $begin = strtotime($begin_time);
            $end   = strtotime($end_time);
            $map_join['t.time'] = array('between',["$begin","$end"]);
        }
        //获取钱包类型---搜索用
        $wallet_type = wallet_type();                                       
        $list  =  db('transfer')
                    ->alias('t')
                    ->where($map_join)
                    ->join('user u','u.id = t.in_uid')
                    ->join('user us','us.id = t.out_uid')
                    ->field('t.*,u.user_name as in_name,us.user_name as out_name')
                    ->order('t.id DESC')->paginate(15, false, ['page' => $page,'query' => Request::instance()->param()]);
        // 统计总量
        $transfer['sum_num'] = db('transfer')                                   
                    ->alias('t')
                    ->where($map_join)
                    ->join('user u','u.id = t.in_uid')
                    ->join('user us','us.id = t.out_uid')
                    ->sum('num');                                           
        return $this->fetch('index', ['list' => $list, 'keyword' => $keyword, 'wallet_type' => $wallet_type, 'wallet_type_id' => $wallet_type_id, 'transfer' => $transfer,'begin_time'=>$begin_time==''? '':$begin_time,'end_time'=>$end_time==''? '':$end_time]);
    }
}